package com.polaris.live.popup

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.TaskRewardAdapter
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.DialogTaskRewardBinding
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.UserTaskReceiveRewardResp
import com.polaris.live.resp.go_resp.UseConsumableRequest
import com.polaris.live.resp.go_resp.UseDressUpBean
import com.polaris.live.resp.go_resp.UseNumberRequest
import com.polaris.live.translation.R.string

/**
 * TaskRewardPopup 任务奖励
 *
 * @author Created by 天晴 on 2024/10/30 16:43
 * @since 1.0.0
 **/
class TaskRewardPopup(
    context: Context,
    private val mViewModel: BaseViewModel,
    private val data: UserTaskReceiveRewardResp,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_task_reward
    }

    private lateinit var mBinding: DialogTaskRewardBinding

    val mAdapter by lazy { TaskRewardAdapter() }
    override fun onCreate() {
        super.onCreate()
        mBinding = DialogTaskRewardBinding.bind(popupImplView)
        val showSize = when {
            data.rewards.size >= 3 -> 3
            data.rewards.isNotEmpty() -> data.rewards.size
            else -> 3
        }
        mBinding.apply {
            recycler.apply {
                layoutManager = GridLayoutManager(context, showSize, GridLayoutManager.VERTICAL, false)
                adapter = mAdapter
            }
            val str = data.explain ?: ""
            if (str.isEmpty()) {
                tipsTv.visibility = View.GONE
            } else {
                tipsTv.text = str
            }
            if (data.backpackId != null) {
                wear.visibility = VISIBLE
                wear.setOnSingleClickListener {
                    userBackpack()
                }
            }
            ok.setOnSingleClickListener {
                dismiss()
            }
        }
        mAdapter.setList(data.rewards)
    }

    private fun userBackpack() {
        data.backpackId?.let { backpackId ->
            when (data.backpackType) {
                CommonConst.DressUp.TOOL -> {
                    useConsumable(backpackId)
                }

                CommonConst.DressUp.BEAUTIFUL_NUMBER -> {
                    useNumber(backpackId, AppConst.YESLONG)
                }

                else -> {
                    useDressUp(backpackId, AppConst.YES)
                }
            }
        }

    }

    /**
     * 使用装扮
     */
    private fun useDressUp(
        backpackId: Long,
        useStatus: Int,
    ) {
        val useDressUp = UseDressUpBean(backpackId, useStatus)
        mViewModel.requestLoading({ userService.useDressUp(useDressUp) }, {
            ToastGravity.showGravity(StringUtils.getString(string.successfully_worn))
            dismiss()
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })
    }

    /**
     * 使用道具
     */
    private fun useConsumable(
        backpackId: Long,
    ) {
        val useDressUp = UseConsumableRequest(backpackId, null, null)
        mViewModel.requestLoading({ userService.useConsumable(useDressUp) }, {
            ToastGravity.showGravity(StringUtils.getString(string.successfully_worn))
            dismiss()
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })
    }

    /**
     * 使用靓号
     */
    private fun useNumber(
        userNumber: Long, useStatus: Long,
    ) {
        val useDressUp = UseNumberRequest(userNumber, useStatus)
        mViewModel.requestLoading({ userService.useNumber(useDressUp) }, {
            ToastGravity.showGravity(StringUtils.getString(string.successfully_worn))
            dismiss()
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    override fun getPopupWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    companion object {

        fun showPop(context: Context, mViewModel: BaseViewModel, data: UserTaskReceiveRewardResp) {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .hasShadowBg(true)
                .asCustom(TaskRewardPopup(context, mViewModel, data))
                .show()
        }

    }

}